<?php
namespace app\admin\controller;

use think\Db;

class Publics extends Base
{

    /**
     * 退出
     */
    public function logOut()
    {
        $adminUser = new \app\admin\model\AdminUser();
        $adminUser->logOut();
        r(url('Publics/logIn'));
    }

    /**
     * 登陆
     */
    public function logIn()
    {
        $adminUser = new \app\admin\model\AdminUser();
        if ($adminUser->isLogin()) {
            r(url('Index/index'));
        }
        if (is_post()) {
            $username = input('username');
            $password = input('password');
            $code = input('code');
            $is_remember = input('is_remember') ? true : false;
            
            // 验证
            $rule = [
                'username' => 'require',
                'password' => 'require',
                'code' => 'require'
            ];
            $msg = [
                'username.require' => '用户名必填',
                'password.require' => '密码必填',
                'code.require' => '验证码必填'
            ];
            $data = [
                'username' => $username,
                'password' => $password,
                'code' => $code
            ];
            com_validate($rule, $msg, $data, true);
            
            // 验证码
            $v = new \org\Verify();
            if (! $v->check($code)) {
                com_error([
                    'code' => '验证码错误!'
                ]);
                com_check();
            }
            
            // 登录
            $adminUser->logIn($username, $password, $is_remember);
            $config = [
                'suc' => [
                    'url' => url('Index/index')
                ]
            ];
            com_check($config);
        }
        
        $this->view->engine->layout(false);
        return $this->fetch();
    }

    /**
     * 验证码
     */
    public function verify()
    {
        $config = [
            'codeSet' => '123456789',
            'expire' => 1800,
            'fontSize' => 18, // 验证码字体大小(px)
            'useCurve' => false, // 是否画混淆曲线
            'useNoise' => false, // 是否添加杂点
            'imageH' => 40, // 验证码图片高度
            'imageW' => 140, // 验证码图片宽度
            'length' => 4, // 验证码位数
            'fontttf' => '', // 验证码字体，不设置随机获取
            'bg' => [
                243,
                251,
                254
            ], // 背景颜色
            'reset' => true
        ] // 验证成功后是否重置
;
        $verify = new \org\Verify($config);
        return $verify->entry();
    }
}
